import { CallID } from "../CallID";
import { Common_UIPath } from "../common/Common_Define";
import { Cfg } from "../config/Cfg";
import { Const } from "../config/Const";
import { Notifier } from "../framework/Notifier";
import { UIManager } from "../framework/UIManager";
import { Util } from "../utils/Util";
import { GameVoManager } from "./GameVoManager";

export default class GuajiManager {

    private static _instance: GuajiManager = null;
    public static get inst(): GuajiManager {
        return this._instance || (this._instance = new GuajiManager());
    }

    public rewardList: Array<number> = [];
    public goldInCome: number = 0;//当局收入
    public diamondInCome: number = 0;    //当局钻石收入

    getDrop(count: number = 1) {
        console.log("count", count);
        count *= 6;
        let is5bei = GameVoManager.getInstance.myUserVo.guaji5bei;
        this.rewardList = [10001, 10002, 10003, 20001];
        // this.goldInCome = Util.random(100, 500);
        this.goldInCome = GameVoManager.getInstance.getBattle() * (count / 6 * 2);
        this.diamondInCome = Util.random(10, 40);
        if (is5bei) {
            this.goldInCome *= 5;
        }

        let lvl = GameVoManager.getInstance.myUserVo.top_level;
        if (lvl == 0) {
            lvl = 10;
        }
        for (let j = 0; j < count; j++) {
            for (let i = 1; i < lvl; i++) {
                let goodslist = [];
                let droplist = [];
                let dropnum = [];

                let data = Cfg.Barrier.get(i);
                if (!data) {
                    continue;
                }
                let dropid = data.normalDropId;
                goodslist = Notifier.call(CallID.Drop_GetDropPoolRewardById, dropid);
                // console.log("goodslist======" + dropid, goodslist);

                let goodsLen = goodslist.length;
                let gold = 0;
                let diamond = 0;
                let quality = [0, 0, 0, 0];
                for (let i = 0; i < goodsLen; i++) {
                    let goodsType: Const.dropType = goodslist[i][0];
                    let goodsNum = goodslist[i][2];
                    let goodsTypeId = goodslist[i][1];
                    if (goodsType == Const.dropType.Gold) {
                        if (goodsTypeId == 1) {
                            gold += goodsNum;
                            droplist.push(1);
                            dropnum.push(goodsNum);
                            if (is5bei) {
                                gold += (goodsNum * 4);
                                droplist.push(1);
                                dropnum.push(goodsNum);
                            }
                        } else if (goodsTypeId == 2) {
                            diamond += goodsNum;
                            droplist.push(2);
                            dropnum.push(goodsNum);
                            if (is5bei) {
                                diamond += (goodsNum * 4);
                                droplist.push(2);
                                dropnum.push(goodsNum);
                            }
                        }
                    } else if (goodsType == Const.dropType.Equip) {
                        for (let k = 0; k < goodsNum; k++) {
                            droplist.push(goodsTypeId);
                            dropnum.push(1);
                            GameVoManager.getInstance.addEquip(goodsTypeId);
                            this.rewardList.push(goodsTypeId);
                            if (is5bei) {
                                for (let ik = 0; ik < 4; ik++) {
                                    droplist.push(goodsTypeId);
                                    dropnum.push(1);
                                    GameVoManager.getInstance.addEquip(goodsTypeId);
                                    this.rewardList.push(goodsTypeId);
                                }
                            }
                        }
                    }
                }
                if (gold) {
                    this.goldInCome += gold;
                }
                if (diamond) {
                    this.diamondInCome += diamond;
                }
            }
        }

        GameVoManager.getInstance.setGold(this.goldInCome);
        GameVoManager.getInstance.setDiamond(this.diamondInCome);
        UIManager.Open(Common_UIPath.GuajiRewardView);
    }

}